import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../store/user'
import Home from '../views/Home.vue'
import Login from '../views/auth/Login.vue'
import Register from '../views/auth/Register.vue'
import Profile from '../views/auth/Profile.vue'
import MyDownloads from '../views/materials/MyDownloads.vue'
import Balance from '../views/payment/Balance.vue'
import AdminLayout from '../views/admin/AdminLayout.vue'
import Dashboard from '../views/admin/Dashboard.vue'
import ImageManagement from '../views/admin/ImageManagement.vue'
import Logs from '../views/admin/Logs.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/register',
    redirect: '/auth/register'
  },
  {
    path: '/auth/login',
    name: 'Login',
    component: Login,
    meta: { guestOnly: true }
  },
  {
    path: '/auth/register',
    name: 'Register',
    component: Register,
    meta: { guestOnly: true }
  },
  {
    path: '/auth/profile',
    name: 'Profile',
    component: Profile,
    meta: { requiresAuth: true }
  },
  {
    path: '/materials/my-downloads',
    name: 'MyDownloads',
    component: MyDownloads,
    meta: { requiresAuth: true }
  },
  {
    path: '/payment/balance',
    name: 'Balance',
    component: Balance,
    meta: { requiresAuth: true }
  },
  {
    path: '/materials',
    name: 'Materials',
    component: () => import('../views/Materials.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/payment/cart',
    name: 'Cart',
    component: () => import('../views/materials/Cart.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/materials/orders',
    name: 'MaterialOrderList',
    component: () => import('../views/materials/MaterialOrderList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/materials/order/:orderNumber',
    name: 'MaterialOrderDetail',
    component: () => import('../views/materials/MaterialOrderDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/payment/material/:orderNumber',
    name: 'MaterialPayment',
    component: () => import('../views/payment/MaterialPayment.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        name: 'AdminDashboard',
        component: Dashboard
      },
      {
        path: 'materials',
        name: 'AdminMaterials',
        component: ImageManagement
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('../views/admin/users/List.vue')
      },
      {
        path: 'finance/recharges',
        name: 'AdminRecharges',
        component: () => import('../views/admin/finance/Recharges.vue')
      },
      {
        path: 'finance/consumptions',
        name: 'AdminConsumptions',
        component: () => import('../views/admin/finance/Consumptions.vue')
      },
      {
        path: 'logs',
        name: 'AdminLogs',
        component: Logs
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫在 router/guard.js 中配置

export default router